﻿
Imports System.Drawing.Imaging
Imports System.Drawing
Public Class Site
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        adRotator.DataSource = FetchAdsFromDB()
        adRotator.DataBind()
    End Sub

    Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        adRotator.DataSource = FetchAdsFromDB()
        adRotator.DataBind()
    End Sub

    Private Function FetchAdsFromDB() As Object
        'Dim defaultSqlBind As String = "(select top 1 'Blog User : ' & username & ' has posted  maximum number of Posting: '  & max(PostedInfo) & ' And got Gift Vochures from Cricket Forum. Thanks for sharing Information. ' as AlternateText   from userprofile group by username order by max(PostedInfo) desc) Union (select top 1 'The Blog User :' & username & ' has posted  maximum number of Comments: '  & max(PostedComments) & ' And got Gift Vochures from Cricket Forum. Thanks for sharing Information. ' as AlternateText    from userprofile group by username order by max(PostedComments) desc) Union (select top 1 'The Blog User : ' & username & ' has answered  maximum number of Questions: '  & max(QuariesAnswered) & ' And got Gift Vochures from Cricket Forum. Thanks for sharing Information. ' as AlternateText  from userprofile group by username order by max(QuariesAnswered) desc)  Union (select top 1 'The Blog User : ' & username & ' comments are liked by maximum number: '  & max(CommentsLikedByOthers) & ' And got Gift Vochures from Cricket Forum. Thanks for sharing Information. ' as AlternateText  from userprofile group by username order by max(CommentsLikedByOthers) desc) "
        Dim defaultSqlBind As String = "(select top 1 'Blog User : ' & username & ' has posted  maximum number of Posting : '  & max(PostedInfo) & ' and got Gift Vochures from Cricket Forum. ' as AlternateText , '~/Pictures/General/Adr1.jpg' as ImageUrl  from userprofile group by username order by max(PostedInfo) desc) Union (select top 1 'Blog User : ' & username & ' has posted  maximum number of Comments : '  & max(PostedComments) & ' and got Gift Vochures from Cricket Forum. ' as AlternateText  , '~/Pictures/General/Adr2.jpg' as ImageUrl  from userprofile group by username order by max(PostedComments) desc) Union (select top 1 'Blog User : ' & username & ' has answered  maximum number of Questions : '  & max(QuariesAnswered) & ' and got Gift Vochures from Cricket Forum. '  as AlternateText ,  '~/Pictures/General/Adr3.jpg' as ImageUrl  from userprofile group by username order by max(QuariesAnswered) desc)  Union (select top 1 'Blog User : ' & username & ' comments are liked by maximum number : '  & max(CommentsLikedByOthers) & ' and got Gift Vochures from Cricket Forum. ' as AlternateText,  '~/Pictures/General/Adr4.jpg' as ImageUrl from userprofile group by username order by max(CommentsLikedByOthers) desc) "
        Dim ds = DataAccessLayer.ExecuteDataSet(defaultSqlBind)
        Dim dt As DataTable
        dt = ds.Tables(0)
        For i = 0 To dt.Rows.Count - 1
            If (i = 0) Then
                TextOnImage("~/Pictures/General/Adr1i.jpg", "~/Pictures/General/Adr1.jpg", dt.Rows.Item(0)("AlternateText").ToString(), ImageFormat.Jpeg, New Font("Arial", 12, FontStyle.Bold), Color.Purple, New Point(10, 10))
            End If
            If (i = 1) Then
                TextOnImage("~/Pictures/General/Adr2i.jpg", "~/Pictures/General/Adr2.jpg", dt.Rows.Item(1)("AlternateText").ToString(), ImageFormat.Jpeg, New Font("Arial", 12, FontStyle.Bold), Color.Purple, New Point(10, 10))
            End If
            If (i = 2) Then
                TextOnImage("~/Pictures/General/Adr3i.jpg", "~/Pictures/General/Adr3.jpg", dt.Rows.Item(2)("AlternateText").ToString(), ImageFormat.Jpeg, New Font("Arial", 12, FontStyle.Bold), Color.Purple, New Point(10, 10))
            End If
            If (i = 3) Then
                TextOnImage("~/Pictures/General/Adr4i.jpg", "~/Pictures/General/Adr4.jpg", dt.Rows.Item(3)("AlternateText").ToString(), ImageFormat.Jpeg, New Font("Arial", 12, FontStyle.Bold), Color.Purple, New Point(10, 10))
            End If
        Next
        Return ds
    End Function

    Protected Sub adRotator_AdCreated(sender As Object, e As System.Web.UI.WebControls.AdCreatedEventArgs) Handles AdRotator.AdCreated
        adRotator.ControlStyle.Font.Bold = True
        adRotator.CssClass = "AddRotatorBanner"

    End Sub

    Protected Sub adRotator_Init(sender As Object, e As EventArgs) Handles AdRotator.Init
        adRotator.CssClass = "AddRotatorBanner"
    End Sub


    Private Sub TextOnImage(ByVal OldImage As String, ByVal NewImage As String, ByVal Text As String, ByVal Format As ImageFormat, ByVal Font As Font, ByVal Color As Color, ByVal Position As Point)
        Dim TmpSize As System.Drawing.Size
        Dim Image As Image = System.Drawing.Image.FromFile(Server.MapPath(OldImage))
        Dim Brush As New SolidBrush(Color)

        'Read Image Dimensions
        TmpSize.Height = Image.Height
        TmpSize.Width = Image.Width

        'Create a new Bitmap Object
        Dim NewBitmap As New System.Drawing.Bitmap(Image, TmpSize)

        'Create a new Graphic Object
        Dim Graphic As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(NewBitmap)

        'Draw String on Image
        Graphic.DrawString(Text, Font, Brush, Position)

        'Save new Image
        NewBitmap.Save(Server.MapPath(NewImage))

        Graphic.Dispose()
        NewBitmap.Dispose()
    End Sub
End Class